package com.xjscrm.console.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: jianghan
 * @Date: 2021/12/8 13:54
 * @Description:
 */
@Component
public class RatelimitService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DefaultRedisScript<Boolean> redisScript;

    public boolean inRateLimit(String key, Integer limitCount, Integer time) {
        List<String> keys = new ArrayList<>();
        keys.add(key);
        return (Boolean) redisTemplate.execute(redisScript, keys, limitCount, time);
    }
}
